//package com.ctrip.frame;
//
//import com.reformer.commons.bo.Page;
//import com.rfca.business.MessageQueue;
//import com.rfca.business.MessageService;
//import com.rfca.business.UserService;
//import com.rfca.task.MessageListTask;
//import com.rfca.task.WebSocketTask;
//import com.rfca.utils.Jacob4TTS;
//import com.rfca.vo.CallMessage;
//import com.rfca.vo.GlobalData;
//import lombok.extern.slf4j.Slf4j;
//
//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.MouseAdapter;
//import java.awt.event.MouseEvent;
//import java.util.List;
//
//@Slf4j
//public class MainFrame extends JFrame {
//    static DefaultListModel<CallMessage> listModel;
//    OrderFrame historyFrame;
//    JTextArea msgAarea;
//    JPanel panelTop;
//    JLabel labelWelcome, labelLogout, lableHistory;
//    JScrollPane scrollPaneMsgList;
//    JTextPane textPane;
//    JList listMsgs;
//
//    public MainFrame() {
//        super("呼叫监控中心");
//        initGUI();
//        WebSocketTask.start();
//        MessageListTask.start();
//    }
//
//    /**
//     * 获取最近50条消息
//     *
//     * @return
//     */
//    private static List<CallMessage> getMessages() {
//        List<CallMessage> messages = MessageService.list(new Page(50, 1), null, null, null, null);
//        if (messages != null) {
//            messages.forEach(message -> {
//                message.setSendTime(message.getSendTime().substring(5, 16));
//            });
//        }
//        return messages;
//    }
//
//    /**
//     * 首次初始化消息列表
//     */
//    public static void initJList() {
//        List<CallMessage> messages = getMessages();
//        if (messages != null) {
//            MessageQueue.init(messages);
//            renderJList(messages);
//        }
//    }
//
//    /**
//     * 重置下次列表
//     */
//    public static void resetJList() {
//        List<CallMessage> messages = getMessages();
//        if (messages != null) {
//            MessageQueue.init(messages);
//            listModel.clear();
//            renderJList(messages);
//        }
//    }
//
//    /**
//     * 填充消息列表
//     *
//     * @param messages
//     */
//    private static void renderJList(List<CallMessage> messages) {
//        messages.forEach(message -> {
//            listModel.addElement(message);
//        });
//    }
//
//    public static void addMessage2List(CallMessage message) {
//        message.setSendTime(message.getSendTime().substring(5, 16));
//        listModel.add(0, message);
//        if (listModel.size() > MessageQueue.SIZE) {
//            listModel.removeRange(MessageQueue.SIZE, listModel.size() - 1);
//        }
//        //语音
//        Jacob4TTS.speak(String.format("%s,在,%s,%s", message.getSender(), message.getLocation(), message.getMessage()));
//    }
//
//    public static void setState(Integer messageId) {
//        for (int i = 0; i < listModel.getSize(); i++) {
//            CallMessage message = listModel.get(i);
//            if (message.getId().equals(message) && message.getHandleState().intValue() == 0) {
//                message.setHandleState(1);
//            }
//        }
//    }
//
//    void initGUI() {
////        container.setBackground(Color.ORANGE);
////        Draft[] drafts = {new Draft_6455()};
////        draft = new JComboBox(drafts);
////        container.add(draft);
//        panelTop = new JPanel();
//        panelTop.setBackground(new Color(131, 169, 233));
//        panelTop.setBounds(0, 0, 445, 33);
//        panelTop.setLayout(null);
//
//        labelWelcome = new JLabel("欢迎您：");
//        labelWelcome.setBounds(2, 1, 90, 32);
//        labelWelcome.setFont(new Font(Theme.fontFamily, Font.PLAIN, 20));
//
//        textPane = new JTextPane();
//        textPane.setText(GlobalData.username);
//        textPane.setBounds(92, 1, 70, 32);
//        textPane.setBackground(new Color(131, 169, 233));
//        textPane.setFont(new Font("黑体", Font.BOLD, 20));
//        textPane.setEditable(false);
//        textPane.setSelectionColor(Color.ORANGE);
//        textPane.setSelectedTextColor(Color.BLUE);
//
//        lableHistory = new JLabel("<html><u>[历史消息]</u></html>");
//        lableHistory.setBounds(200, 1, 100, 32);
//        lableHistory.setFont(new Font(Theme.fontFamily, Font.PLAIN, 20));
//        lableHistory.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//        lableHistory.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                if (historyFrame == null) {
//                    historyFrame = new OrderFrame();
//                    historyFrame.setVisible(true);
//                } else if (!historyFrame.isShowing()) {
//                    historyFrame.setVisible(true);
//                }
//            }
//        });
//
//        labelLogout = new JLabel("<html><u>[退出]</u></html>");
//        labelLogout.setBounds(360, 1, 70, 32);
//        labelLogout.setFont(new Font(Theme.fontFamily, Font.PLAIN, 20));
//        labelLogout.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//        labelLogout.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                exit();
//            }
//        });
//        panelTop.add(labelWelcome);
//        panelTop.add(textPane);
//        panelTop.add(lableHistory);
//        panelTop.add(labelLogout);
//
//        Image image = this.getToolkit().getImage(getClass().getClassLoader().getResource("images/logo.png"));
//        this.setIconImage(image);
//
//
//        listModel = new DefaultListModel();
//        initJList();
//        listMsgs = new JList<>(listModel);
//        listMsgs.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
////        listMsgs.setFixedCellWidth(348);
//        listMsgs.setFixedCellHeight(33);
//        listMsgs.setCellRenderer(new MyListCellRenderer());
////        listMsgs.addMouseListener(new MouseAdapter() {
////            @Override
////            public void mouseClicked(MouseEvent e) {
////                if(e.getClickCount()==2){
////                    CallMessage message = (CallMessage) listMsgs.getSelectedValue();
////                    MessageQueue.handle(message.getId());
////                    MessageService.handle(message.getId());
////                }
////            }
////        });
//        //debug
//
//        scrollPaneMsgList = new JScrollPane(listMsgs);
//        scrollPaneMsgList.getVerticalScrollBar().setUI(new CBScrollBarUI());
//        scrollPaneMsgList.setHorizontalScrollBarPolicy(
//                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//        scrollPaneMsgList.setVerticalScrollBarPolicy(
//                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//        scrollPaneMsgList.setBounds(0, 33, 445, 844);
//
//        JScrollBar scrollBar = scrollPaneMsgList.getVerticalScrollBar();
//        if (scrollBar != null) {
//            scrollBar.setBackground(new Color(153, 153, 153));
//            scrollBar.setValue(scrollBar.getMaximum() / 2);
//        }
////        JViewport viewport = scrollPaneMsgList.getViewport();
////        viewport.setViewSize(new Dimension(348, 852));
////        viewport.setBounds(2,35,348,852);
////        viewport.setView(listMsgs);
//        //layout of JScrollPane must be a ScrollPaneLayout
////        scrollPaneMsgList.setLayout(new FlowLayout());
//
//        Container container = getContentPane();
//        container.add(panelTop);
//        container.add(scrollPaneMsgList);
//
//        setSize(450, 905);
//        setResizable(false);
//        setLocationRelativeTo(null);
//        setLayout(null);
////        pack();
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
//        //启动订阅监听线程
////        new MonitorThread().start();
////        WebSocketClient client = null;
//
////        client.run();
//
//    }
//    //    @Override
////    public void actionPerformed(ActionEvent event) {
////        try {
////            String url = String.format("ws://%s:%d/call-monitor/ws/monitor?token=%s", ServerConfig.Server.host, ServerConfig.Server.port, GlobalData.accessToken);
//////            url="ws://localhost:8080/websocket/ws?uid=1111";
////            log.info(url);
////            client = new WebSocketClient(new URI(url) ,(Draft) draft.getSelectedItem() ){
////                @Override
////                public void onOpen(ServerHandshake serverHandshake) {
////                    log.info("连接已经打开");
////                }
////
////                @Override
////                public void onMessage(String s) {
////                    log.info("收到消息：{}", s);
////                    msgAarea.append(s);
////                }
////
////                @Override
////                public void onClose(int code, String reason, boolean remote) {
////                    // The codecodes are documented in class org.java_websocket.framing.CloseFrame
////                    log.info("Connection closed by {} ,Code:{},Reason:{}", remote ? "remote peer" : "us", code, reason);
////                }
////
////                @Override
////                public void onError(Exception e) {
////                    log.error("链接发生错误已关闭=>{}", e.toString());
////                }
////            };
////        } catch (URISyntaxException e) {
////            log.error(e.toString(), e);
////        }
////        client.connect();
////        client.send("Hello");
////    }
//
//    void exit() {
//        try {
//            WebSocketTask.stop();
//            UserService.logout(GlobalData.username);
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//        Jacob4TTS.release();
//        System.exit(0);
//    }
//}
